package com.xysq.adapter;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.rockcent.model.MessageBO;
import com.xysq.lemon.R;
import com.xysq.util.UseUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 消息列表的Adapter
 */
public class MessageListAdapter extends BaseDataAdapter<MessageBO> {

    /*    private LayoutInflater mInflater;
        private List<Inmessage> mList;*/
    private Context mContext;

    public MessageListAdapter(Context context) {
        super(context);
        mContext = context;
    }

   /* public MessageListAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        mList = new ArrayList<>();
        mContext = context;
    }

    public void append(List<Inmessage> list) {
        if (CollectionUtil.isNullOrEmpty(list)) {
            return;
        }
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clear() {
        if (mList.size() == 0) {
            return;
        }
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        //返回信息的Id
        return mList.get(position).getId();
    }*/

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_message_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final MessageBO message = itemList.get(position);

        /*String typeStr = message.getIsReaded() == 0 ? "未读" : "已读";
        holder.mType.setText(typeStr);*/
       /* MessageType messageType = message.getType();
        String typeStr = mContext.getResources().getString(R.string.txt_message_system);
        if (messageType != null) {
            switch (messageType) {
                case SYSTEM:
                    typeStr = mContext.getResources().getString(R.string.txt_message_system);
                    break;
                case TRANSACTION:
                    typeStr = mContext.getResources().getString(R.string.txt_message_trade);
                    break;
                case REDPACKET:
                    typeStr = mContext.getResources().getString(R.string.txt_message_red_packet);
                    break;
                case DRAWBACK:
                    typeStr = mContext.getResources().getString(R.string.txt_message_drawback);
                    break;
                case OTHER:
                    typeStr = mContext.getResources().getString(R.string.txt_message_other);
                    break;
                default:
                    break;
            }
        }

        holder.mType.setText(typeStr);
        holder.titleTv.setText(message.getTitle());*/
        String content = message.getContent();
//        Log.d("test", "替换前：" + content);
        content = content.replaceAll("(<a ([^>]*?)>)(.*?)(</a>)", "");
//        Log.d("test", "替换后：" + content);
/*        if (content.indexOf(mContext.getResources().getString(R.string.text_query_trade)) != -1) {
            content = content.replaceAll(mContext.getResources().getString(R.string.text_query_trade), "");
        }*/
        holder.contentTv.setText(Html.fromHtml(content));
        holder.sendTimeTv.setText(UseUtil.timeToDate(message.getCreateDate()));

        return convertView;
    }


    class ViewHolder {

        /*        //消息的种类
                @InjectView(R.id.tv_type)
                TextView mType;
                //消息的标题
                @InjectView(R.id.tv_title)
                TextView titleTv;*/
        //消息的正文
        @InjectView(R.id.tv_content)
        TextView contentTv;
        //消息的发送时间
        @InjectView(R.id.tv_sendTime)
        TextView sendTimeTv;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}

